package cn.licoy.wdog.core.service.hospital.impl;

import cn.licoy.wdog.common.bean.ResponseCode;
import cn.licoy.wdog.common.bean.ResponseResult;
import cn.licoy.wdog.common.exception.RequestException;
import cn.licoy.wdog.core.config.jwt.JwtToken;
import cn.licoy.wdog.core.dto.SplitPageDTO;
import cn.licoy.wdog.core.dto.hospitalUser.DetectionInfoDto;
import cn.licoy.wdog.core.dto.hospitalUser.PageDto;
import cn.licoy.wdog.core.entity.hospital.DetectionInfo;
import cn.licoy.wdog.core.entity.hospital.HospitalUser;
import cn.licoy.wdog.core.entity.system.SysUser;
import cn.licoy.wdog.core.mapper.hospital.DetectionInfoMapper;
import cn.licoy.wdog.core.service.hospital.DetectionInfoService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Sunne
 * Create time 2018/6/22 17:27
 * @return null
 **/
@Service
@Transactional
public class DetectionInfoServiceImpl extends ServiceImpl<DetectionInfoMapper,DetectionInfo> implements DetectionInfoService {

    @Override
    public void add(DetectionInfoDto detectionInfoDto,String id) {
        if(detectionInfoDto.getLdi()!=null||detectionInfoDto.getLdi().size()>0){
            for (int i=0;i<detectionInfoDto.getLdi().size();i++){

                DetectionInfo detectionInfo = new DetectionInfo();
                detectionInfo = detectionInfoDto.getLdi().get(i);
                detectionInfo.setIsException(this.isexception(detectionInfo));
                String format = "yyyy-MM-dd HH:mm:ss";
                SimpleDateFormat sdf = new SimpleDateFormat(format);
                String detectionDate = sdf.format(detectionInfoDto.getLdi().get(i).getDetectionDate());
                detectionInfo.setCreateDate(new Date());
                detectionInfo.setDetectionDate(detectionInfoDto.getLdi().get(i).getDetectionDate());
                detectionInfo.setRemake("1");
                detectionInfo.setCreateUserId(id);
                this.insert(detectionInfo);
            }
        }else {
            throw RequestException.fail("数据不能为空");
        }
    }

    @Override
    public Page<DetectionInfo> selectToCondition(String where,String field,Integer currage,Integer pagesize,String id) {
        if(where!=null&&where!="") {
            if (currage == null||currage == 0) {
                currage = 1;
            }
            if (pagesize == null||pagesize == 0 ) {
                pagesize = 20;
            }
            EntityWrapper<DetectionInfo> wrapper = new EntityWrapper<>();
            wrapper.orderBy("id",true).eq(field,where)
            .eq("create_user_id",id);
            Page<DetectionInfo> detectionInfoPage = this.selectPage(new Page<>(currage,pagesize),wrapper);
            return detectionInfoPage;
        }else {
            throw RequestException.fail("查询参数都不能为空");
        }

    }

    @Override
    public Page<DetectionInfo> selectToDate(long startTime,long endTime,Integer currage,Integer pagesize,String id) {
        if(!"".equals(startTime)&&!"".equals(endTime)){
            if(currage==null||currage==0){
                currage = 1;
            }
            if(pagesize==null||pagesize==0){
                pagesize = 20;
            }
            String format = "yyyy-MM-dd HH:mm:ss";
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            String startDate = sdf.format(startTime);
            String endDate = sdf.format(endTime);
            EntityWrapper<DetectionInfo> wrapper = new EntityWrapper<>();
            wrapper.orderBy("id",false)
                    .eq("create_user_id",id)
                    .between("detection_date",startDate,endDate);
            Page<DetectionInfo> detectionInfoPage = this.selectPage(new Page<>(currage,pagesize),wrapper);
            if (detectionInfoPage.getRecords().size()>0){
                return detectionInfoPage;
            }else {
                throw RequestException.fail("当前区间段无数据");
            }

        }else {
            throw RequestException.fail("时间不能为空");
        }

    }

    @Override
    public Page<DetectionInfo> selectToAll(PageDto pageDto,String userid) {
            if(pageDto.getCurrentPage()==null||pageDto.getCurrentPage()==0){
                pageDto.setCurrentPage(1);
            }
            if(pageDto.getPageSize()==null||pageDto.getPageSize()==0){
                pageDto.setPageSize(20);
            }
            if(userid!=null&&"1032537020441780225".equals(userid)){
                EntityWrapper<DetectionInfo> wrapper = new EntityWrapper<>();
                wrapper.orderBy("detection_date",false);
                Page<DetectionInfo> page = this.selectPage(new Page<>(pageDto.getCurrentPage(),pageDto.getPageSize()),wrapper);
                return page;
            }else {
                EntityWrapper<DetectionInfo> wrapper = new EntityWrapper<>();
                wrapper.orderBy("detection_date",false)
                        .eq("create_user_id",userid);
                Page<DetectionInfo> page = this.selectPage(new Page<>(pageDto.getCurrentPage(),pageDto.getPageSize()),wrapper);
                return page;
            }

    }

    @Override
    public List<String> findReginByUsername(HospitalUser user) {

        List<DetectionInfo> ls = this.selectList(new EntityWrapper<DetectionInfo>()
                .eq("create_user_id",user.getId())
                .setSqlSelect("region")
                .groupBy("region")
        );
        List<String> al = new ArrayList<>();
        ls.forEach(v->al.add(v.getRegion()));
        return al;
    }

    public Page<DetectionInfo> selectReginByUserName(PageDto pageDto) {
        return null;
    }



    /**
     * 检测结果处理，正常返回0，异常返回1
     **/
    public String isexception(DetectionInfo detectionInfo){
        if (detectionInfo !=null && !"".equals(detectionInfo)){
            if (!"-".equals(detectionInfo.getLeu())){
                return "1";
            }
            if (!"-".equals(detectionInfo.getNit())){
                return "1";
            }
            if (!"-".equals(detectionInfo.getUbg())){
                return "1";
            }
            if (!"-".equals(detectionInfo.getPro())){
                return "1";
            }
            double ph = Double.valueOf(detectionInfo.getPh());
            if(ph<4.5||ph>8){
                return "1";
            }
            if (!"-".equals(detectionInfo.getBld())){
                return "1";
            }
            double sg = Double.valueOf(detectionInfo.getSg());
            if (sg<1.015||sg>1.025){
                return "1";
            }
            if (!"-".equals(detectionInfo.getKet())){
                return "1";
            }
            if (!"-".equals(detectionInfo.getBil())){
                return "1";
            }
            if (!"-".equals(detectionInfo.getGlu())){
                return "1";
            }
            if (!"-".equals(detectionInfo.getVc())){
                return "1";
            }
            if (detectionInfo.getMa()!=null && !"-".equals(detectionInfo.getMa())){
                return "1";
            }
            if (detectionInfo.getCre()!=null && !"-".equals(detectionInfo.getCre())){
                return "1";
            }
            if (detectionInfo.getCa()!=null && !"-".equals(detectionInfo.getCa())){
                return "1";
            }
            /*此处有BUG 你的幸运女神也没有用 必须要￥1900.00元的妹子*/
            /*double ma = Double.valueOf(detectionInfo.getMa()).longValue();
            if (ma<20){
                return "1";
            }
            double cre = Double.valueOf(detectionInfo.getCre()).longValue();
            if (cre<8.4||cre>13.2){
                return "1";
            }
            double ca = Double.valueOf(detectionInfo.getCa()).longValue();
            if (ca<2.5||ca>7.5){
                return "1";
            }*/
            return "0";
        }else {
            throw RequestException.fail("数据不能为空");
        }
    }
}
